﻿using System;
using KillBill.Business.Entities;
using KillBill.Presentation.Common.WindowsForms.View;
using KillBill.Presentation.Modules.Administration.IView;
using KillBill.Presentation.Modules.Administration.Presenter;

namespace KillBill.Presentation.Modules.Administration.View
{
    public partial class CustomerAddEditView : KillBillWinFormView, ICustomerAddEdit
    {
        private Customer _customer;

        public CustomerAddEditPresenter Presenter { get; set; }

        public bool IsEditing { get; set; }

        public Customer Customer
        {
            get
            {
                _customer.Person = uscPerson1.Person;
                _customer.CustomerID = uscPerson1.Person.PersonID;
                _customer.CustomerRUC = txtCustomerRUC.Text;
                _customer.Reference = RicReference.Text;
                return _customer;
            }
            set
            {
                uscPerson1.Person = value.Person;
                txtCustomerRUC.Text = value.CustomerRUC;
                RicReference.Text = value.Reference;
                _customer = value;
            }
        }

        public CustomerAddEditView()
        {
            InitializeComponent();
            Presenter = new CustomerAddEditPresenter(this);
            Customer = new Customer();
            IsEditing = false;
        }

        public CustomerAddEditView(Customer customer)
        {
            InitializeComponent();
            Presenter = new CustomerAddEditPresenter(this);
            Customer = customer;
            IsEditing = true;
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if(uscPerson1.IsValid && txtCustomerRUC.IsValid)
                Presenter.Save();
            else
                this.ShowMessage("Existen datos incorrectos");
        }
    }
}
